package co.wizardattack.activities;

import co.wizardattack.configuration.Configuration;

import com.example.wizardattack.R;
import com.example.wizardattack.R.id;
import com.example.wizardattack.R.layout;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class MenuActivity extends Activity implements OnClickListener {

	private Configuration config;

	private EditText changeNameEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		config = new Configuration(this);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_menu);

		Button findChickensInFarmButton = (Button) findViewById(R.id.findChickensInFarmButton);
		findChickensInFarmButton.setOnClickListener(this);

		changeNameEditText = (EditText) findViewById(R.id.changeNameEditText);
		changeNameEditText.setOnClickListener(this);
		changeNameEditText.setText(config.username);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.findChickensInFarmButton:
			if(config.username.length() == 0) {
				changeName();
			} else {
				Intent intent = new Intent(this, MenuScrollerActivity.class);
				startActivity(intent);
			}
			break;

		case R.id.changeNameEditText:
			changeName();
			break;
		}
	}

	public void changeName() {

		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		String hola = "";
		alert.setTitle("Your name");
		alert.setMessage("Write someting that identifies you in the farm");
		final EditText input = new EditText(this);
		input.setText(changeNameEditText.getText());
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				changeNameEditText.setText(input.getText());
				config.username = input.getText().toString();
			}
		});

		alert.show();

	}

	@Override
	protected void onPause() {
		config.commitChanges();
		super.onPause();
	}

}
